﻿//************************************************************************//
//文件功能：条码生成辅助类
//创 建 人：薄帅
//创建时间：2012年08月20日
//修 改 人：
//修改时间：
//************************************************************************//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.IO;

/// <summary>
///DrawImageBord 的摘要说明
/// </summary>
public abstract class DrawImageBord
{
    protected virtual string BordRuleName
    {
        get { return string.Empty; }
    }

    protected virtual System.Collections.Hashtable Roles
    {
        get { return new System.Collections.Hashtable(); }
    }

    string drawString;
    int width = 330;   //画布的宽度(可计算)   
    int height = 100;//1CM   
    int unitWidth = 1;   //   

    int currentLocation = 0;

    public DrawImageBord(string s)
    {
        drawString = s;
    }

    public virtual Bitmap GetBitmap()
    {
        Bitmap bm = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(bm);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

        //画布和边的设定     
        g.Clear(Color.White);

        g.DrawRectangle(Pens.White, 0, 0, width, height);
        for (int i = 0; i < drawString.Length; i++)
        {
            this.DrawString(drawString[i].ToString(), g);
        }
        return bm;
    }

    protected virtual void DrawString(string s, Graphics g)
    {
        System.Collections.Hashtable hash = this.Roles;
        object o = hash[s];
        if (o == null) return;
        char[] chars = o.ToString().ToCharArray();
        if (chars.Length > 9) return;
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        SolidBrush witeBrush = new SolidBrush(Color.White);

        for (int i = 0; i < 5; i++)
        {
            //画第一个   0   黑条   
            if (chars[i] == '0')
            {
                Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);
                g.FillRectangle(blackBrush, re1);
                currentLocation += unitWidth;
            }
            else
            {
                Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);
                g.FillRectangle(blackBrush, re1);
                currentLocation += 3 * unitWidth;
            }
            //画第6个     5   白条   
            if ((i + 5) < 9)
            {
                if (chars[i + 5] == '0')
                {
                    Rectangle re1 = new Rectangle(currentLocation, 0, unitWidth, height);
                    g.FillRectangle(witeBrush, re1);
                    currentLocation += unitWidth;
                }
                else
                {
                    Rectangle re1 = new Rectangle(currentLocation, 0, 3 * unitWidth, height);
                    g.FillRectangle(witeBrush, re1);
                    currentLocation += 3 * unitWidth;
                }
            }
        }
        Rectangle re2 = new Rectangle(currentLocation, 0, unitWidth, height);
        g.FillRectangle(witeBrush, re2);
        currentLocation += unitWidth;

    }
}